//
//  ZYWebImageCombinedTask.m
//  ZYWebImage
//
//  Created by wangzhipan on 2025/3/26.
//

#import "ZYWebImageCombinedTask.h"
#import "ZYImageCacheTask.h"
#import "ZYDownloadTask.h"
#import "ZYWebImageManager.h"

@interface ZYWebImageCombinedTask()
@property (nonatomic, assign, readwrite, getter=isCancelled) BOOL cancelled;
@property (nonatomic, weak, nullable) ZYWebImageManager *manager;
@end

@implementation ZYWebImageCombinedTask

- (instancetype)initWithManager:(ZYWebImageManager *)manager
{
    if (self = [super init]) {
        self.manager = manager;
    }
    return self;
}

- (BOOL)isCancelled
{
    @synchronized (self) {
        return _cancelled;
    }
}

- (void)cancel
{
    @synchronized (self) {
        if (_cancelled) {
            return;
        }
        _cancelled = YES;
        if (self.cacheTask) {
            [self.cacheTask cancel];
            self.cacheTask = nil;
        }
        
        if (self.downloadTask) {
            [self.downloadTask cancel];
            self.downloadTask = nil;
        }
        [self.manager safeRemoveRunningTask:self];
    }
}
@end
